package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 添加购物车
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){
        //获取当前用户信息
        Long userId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> lqw=new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,userId);
        //判断添加的是菜品还是套餐
        Long dishId = shoppingCart.getDishId();
        if(dishId!=null){
            //当前添加的是菜品
            lqw.eq(ShoppingCart::getDishId,dishId);
        }else {
            //当前添加的是套餐
            lqw.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        //查询当前用户的购物车
        ShoppingCart cart = shoppingCartService.getOne(lqw);
        //判断当前购物车是否有该菜品或套餐
        if (cart!=null){
            //购物车已有该菜品或套餐
            cart.setNumber(cart.getNumber()+1);
            shoppingCartService.updateById(cart);
        }else{
            //购物车没有该菜品
            shoppingCart.setNumber(1);
            //购物车为空,添加当前用户id
            shoppingCart.setUserId(userId);
            shoppingCartService.save(shoppingCart);
            cart=shoppingCart;
        }
        return R.success(cart);
    }

    /**
     * 查询购物车
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){
        LambdaQueryWrapper<ShoppingCart> lqw=new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        lqw.orderByAsc(ShoppingCart::getCreateTime);
        List<ShoppingCart> list = shoppingCartService.list(lqw);
        return R.success(list);
    }

    /**
     * 清空购物车
     * @return
     */
    @DeleteMapping("/clean")
    public R<String> clean(){
        LambdaQueryWrapper<ShoppingCart> lqw=new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        shoppingCartService.remove(lqw);
        return R.success("清除成功");
    }

    /**
     * 减少购物车
     * @param shoppingCart
     * @return
     */
    @PostMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart){
        //获取当前用户信息
        Long userId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> lqw=new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,userId);
        //判断减少的是菜品还是套餐
        Long dishId = shoppingCart.getDishId();
        if(dishId!=null){
            //当前减少的是菜品
            lqw.eq(ShoppingCart::getDishId,dishId);
        }else {
            //当前减少的是套餐
            lqw.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        //查询当前用户的购物车
        ShoppingCart cart = shoppingCartService.getOne(lqw);
        //判断当前购物车该菜品或套餐的数量
        if (cart.getNumber()==1){
            //再减少即删除菜品
            shoppingCartService.removeById(cart.getId());
        }else{
            //菜品减1
            cart.setNumber(cart.getNumber()-1);
            shoppingCartService.updateById(cart);
        }
        return R.success(cart);
    }
}
